package org.jeecg.modules.qyxx.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.jeecg.modules.qyxx.entity.BgFpHzNdJzdSxygysjxs;
import org.jeecg.modules.qyxx.entity.BgFpHzNdWdxSxygysjxs;
import org.jeecg.modules.qyxx.enumdemo.RedisKeyConstant;
import org.jeecg.modules.qyxx.mapper.BgFpHzNdJzdSxygysjxsMapper;
import org.jeecg.modules.qyxx.service.IBgFpHzNdJzdSxygysjxsService;
import org.jeecg.modules.qyxx.service.IBgSjmkjgOrderService;
import org.jeecg.modules.qyxx.util.DateUtils;
import org.jeecg.modules.qyxx.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 上下游供应商和经销商集中度汇总数据
 * @Author: ZJJ
 * @Date: 2021-06-10
 * @Version: V1.0
 */
@Service
// @DS("multi-datasource1")申报
public class BgFpHzNdJzdSxygysjxsServiceImpl extends MppServiceImpl<BgFpHzNdJzdSxygysjxsMapper, BgFpHzNdJzdSxygysjxs> implements IBgFpHzNdJzdSxygysjxsService {
    /*@Autowired
    private BgFpHzNdJzdSxygysjxsMapper bgFpHzNdJzdSxygysjxsMapper;
    @Autowired
    private IBgSjmkjgOrderService bgSjmkjgOrderService;*/


    @Override
    public List<BgFpHzNdJzdSxygysjxs> fpHzNdJzdSxygysjxs(String djxh, List<String> tjnds, Date jssj) {
        DateUtils utils = DateUtils.getInstance();
        //LambdaQueryWrapper<BgFpHzNdJzdSxygysjxs> wrapper = Wrappers.lambdaQuery(BgFpHzNdJzdSxygysjxs.class);
        // String year = bgSjmkjgOrderService.getNewByDjxh(djxh);
        String year = utils.getYearToStr(jssj);
        //List<BgFpHzNdJzdSxygysjxs> list = new ArrayList<>();
        List<BgFpHzNdJzdSxygysjxs> list = baseMapper.fpHzNdJzdSxygysjxs(djxh, String.join(",", tjnds));
        for (BgFpHzNdJzdSxygysjxs one : list) {
            if (one.getQbgyscgje() != null) {
                one.setQbgyscgje(one.getQbgyscgje().divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP));
            }
            if (one.getQbjxsxsje() != null) {
                one.setQbjxsxsje(one.getQbjxsxsje().divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP));
            }
            if (one.getQsgyscgje() != null) {
                one.setQsgyscgje(one.getQsgyscgje().divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP));
            }
            if (one.getQsjxsxsje() != null) {
                one.setQsjxsxsje(one.getQsjxsxsje().divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP));
            }
            if (year.equals(one.getTjnd())) {
                // String month = bgSjmkjgOrderService.getNewMonthByDjxh(djxh);
                String month = utils.getMonthToStr(jssj);
                String newMonth = month.substring(month.length() - 2);
                one.setTjnd(year + "年(" + newMonth + "月)");
            } else {
                one.setTjnd(one.getTjnd() + "年");
            }
        }
        return list;
        /*int i = 0;
        for (String tjnd : tjnds) {
            wrapper.clear();
            wrapper.eq(BgFpHzNdJzdSxygysjxs::getDjxh, djxh).eq(BgFpHzNdJzdSxygysjxs::getTjnd, tjnd);
            BgFpHzNdJzdSxygysjxs one = bgFpHzNdJzdSxygysjxsMapper.selectOne(wrapper);
            if (one != null) {
                if (one.getQbgyscgje() != null) {
                    one.setQbgyscgje(one.getQbgyscgje().divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP));
                }
                if (one.getQbjxsxsje() != null) {
                    one.setQbjxsxsje(one.getQbjxsxsje().divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP));
                }
                if (one.getQsgyscgje() != null) {
                    one.setQsgyscgje(one.getQsgyscgje().divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP));
                }
                if (one.getQsjxsxsje() != null) {
                    one.setQsjxsxsje(one.getQsjxsxsje().divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP));
                }
                if (year.equals(one.getTjnd())) {
                    // String month = bgSjmkjgOrderService.getNewMonthByDjxh(djxh);
                    String month = utils.getMonthToStr(jssj);
                    String newMonth = month.substring(month.length() - 2);
                    one.setTjnd(year + "年(" + newMonth + "月)");
                }else {
                    one.setTjnd(one.getTjnd() + "年");
                }
                list.add(one);
                i++;
                if (i >= 3) {
                    break;
                }
            }
        }*/
    }
}
